#!/bin/bash

# 普通构建脚本 - 不使用 TurboRepo

echo "🏗️  开始普通构建（不使用 TurboRepo）..."

# 检查是否安装了 Bun
if ! command -v bun &> /dev/null; then
    echo "❌ Bun 未安装，请先安装 Bun: https://bun.sh"
    exit 1
fi

echo "📦 构建共享包..."
cd packages/shared
echo "🔧 安装共享包依赖..."
bun install
echo "🏗️  构建共享包..."
bun run build
if [ ! -d "dist" ]; then
    echo "❌ 共享包构建失败"
    exit 1
fi
echo "✅ 共享包构建完成"
cd ../..

echo "🔧 构建后端 API..."
cd apps/api
echo "🔧 安装 API 依赖..."
bun install
echo "🔧 生成 Prisma 客户端..."
bun run db:generate
echo "🏗️  构建 API..."
bun run build
if [ ! -d "dist" ]; then
    echo "❌ API 构建失败"
    exit 1
fi
echo "✅ API 构建完成"
cd ../..

echo "🌐 构建 Web 前端..."
cd apps/web
echo "🔧 安装 Web 前端依赖..."
bun install
echo "🏗️  构建 Web 前端..."
bun run build
if [ ! -d "dist" ]; then
    echo "❌ Web 前端构建失败"
    exit 1
fi
echo "✅ Web 前端构建完成"
cd ../..

echo "📱 构建移动端前端..."
cd apps/web-mobile
echo "🔧 安装移动端前端依赖..."
bun install
echo "🏗️  构建移动端前端..."
bun run build
if [ ! -d "dist" ]; then
    echo "❌ 移动端前端构建失败"
    exit 1
fi
echo "✅ 移动端前端构建完成"
cd ../..

echo "🎉 所有应用构建完成！"
echo ""
echo "📋 构建结果:"
echo "   ✅ 共享包: packages/shared/dist/"
echo "   ✅ 后端 API: apps/api/dist/"
echo "   ✅ Web 前端: apps/web/dist/"
echo "   ✅ 移动端前端: apps/web-mobile/dist/"
echo ""
echo "现在可以运行 ./start-server.sh 启动服务"
